package tools.wztosql;

import database.DatabaseConnectionWZ;
import java.awt.Point;
import java.io.File;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;

public class DumpMobSkills {

    private MapleDataProvider skill;
    protected boolean hadError = false;
    protected boolean update = false;
    protected int id = 0;
    private Connection con = DatabaseConnectionWZ.getConnection();

    public DumpMobSkills(boolean update) throws Exception {
        this.update = update;
        this.skill = MapleDataProviderFactory.getDataProvider(new File(new StringBuilder().append(System.getProperty("wzpath")).append("/Skill.wz").toString()));
        if (this.skill == null) {
            this.hadError = true;
        }
    }

    public boolean isHadError() {
        return this.hadError;
    }

    public void dumpMobSkills() throws Exception {
        if (!this.hadError) {
            PreparedStatement ps = this.con.prepareStatement("INSERT INTO wz_mobskilldata(skillid, `level`, hp, mpcon, x, y, time, prop, `limit`, spawneffect,`interval`, summons, ltx, lty, rbx, rby, once) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            try {
                dumpMobSkills(ps);
            } catch (Exception e) {
                System.out.println(new StringBuilder().append(this.id).append(" skill.").toString());
                e.printStackTrace();
                this.hadError = true;
            } finally {
                ps.executeBatch();
                ps.close();
            }
        }
    }

    public void delete(String sql) throws Exception {
        PreparedStatement ps = this.con.prepareStatement(sql);
        ps.executeUpdate();
        ps.close();
    }

    public boolean doesExist(String sql) throws Exception {
        PreparedStatement ps = this.con.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        boolean ret = rs.next();
        rs.close();
        ps.close();
        return ret;
    }

    public void dumpMobSkills(PreparedStatement ps) throws Exception {
        if (!this.update) {
            delete("DELETE FROM wz_mobskilldata");
            System.out.println("Deleted wz_mobskilldata successfully.");
        }
        MapleData skillz = this.skill.getData("MobSkill.img");
        System.out.println("Adding into wz_mobskilldata.....");

        for (MapleData ids : skillz.getChildren()) {
            for (MapleData lvlz : ids.getChildByPath("level").getChildren()) {
                this.id = Integer.parseInt(ids.getName());
                int lvl = Integer.parseInt(lvlz.getName());
                if ((!this.update) || (!doesExist(new StringBuilder().append("SELECT * FROM wz_mobskilldata WHERE skillid = ").append(this.id).append(" AND level = ").append(lvl).toString()))) {
                    ps.setInt(1, this.id);
                    ps.setInt(2, lvl);
                    ps.setInt(3, MapleDataTool.getInt("hp", lvlz, 100));
                    ps.setInt(4, MapleDataTool.getInt("mpCon", lvlz, 0));
                    ps.setInt(5, MapleDataTool.getInt("x", lvlz, 1));
                    ps.setInt(6, MapleDataTool.getInt("y", lvlz, 1));
                    ps.setInt(7, MapleDataTool.getInt("time", lvlz, 0));
                    ps.setInt(8, MapleDataTool.getInt("prop", lvlz, 100));
                    ps.setInt(9, MapleDataTool.getInt("limit", lvlz, 0));
                    ps.setInt(10, MapleDataTool.getInt("summonEffect", lvlz, 0));
                    ps.setInt(11, MapleDataTool.getInt("interval", lvlz, 0));

                    StringBuilder summ = new StringBuilder();
                    List<Integer> toSummon = new ArrayList<Integer>();
                    for (int i = 0; (i > -1)
                            && (lvlz.getChildByPath(String.valueOf(i)) != null); i++) {
                        toSummon.add(Integer.valueOf(MapleDataTool.getInt(lvlz.getChildByPath(String.valueOf(i)), 0)));
                    }
                    for (Integer summon : toSummon) {
                        if (summ.length() > 0) {
                            summ.append(", ");
                        }
                        summ.append(String.valueOf(summon));
                    }
                    ps.setString(12, summ.toString());
                    if (lvlz.getChildByPath("lt") != null) {
                        Point lt = (Point) lvlz.getChildByPath("lt").getData();
                        ps.setInt(13, lt.x);
                        ps.setInt(14, lt.y);
                    } else {
                        ps.setInt(13, 0);
                        ps.setInt(14, 0);
                    }
                    if (lvlz.getChildByPath("rb") != null) {
                        Point rb = (Point) lvlz.getChildByPath("rb").getData();
                        ps.setInt(15, rb.x);
                        ps.setInt(16, rb.y);
                    } else {
                        ps.setInt(15, 0);
                        ps.setInt(16, 0);
                    }
                    ps.setByte(17, (byte) (MapleDataTool.getInt("summonOnce", lvlz, 0) > 0 ? 1 : 0));
                    System.out.println(new StringBuilder().append("Added skill: ").append(this.id).append(" level ").append(lvl).toString());
                    ps.addBatch();
                }
            }
        }
        MapleData ids;
        System.out.println("怪物技能ID提取完成!");
    }

    public int currentId() {
        return this.id;
    }

    public static void main(String[] args) {
        boolean hadError = false;
        boolean update = false;
        long startTime = System.currentTimeMillis();
        for (String file : args) {
            if (file.equalsIgnoreCase("-update")) {
                update = true;
            }
        }
        int currentQuest = 0;
        try {
            DumpMobSkills dq = new DumpMobSkills(update);
            System.out.println("Dumping mobskills");
            dq.dumpMobSkills();
            hadError |= dq.isHadError();
            currentQuest = dq.currentId();
        } catch (Exception e) {
            hadError = true;
            e.printStackTrace();
            System.out.println(new StringBuilder().append(currentQuest).append(" skill.").toString());
        }
        long endTime = System.currentTimeMillis();
        double elapsedSeconds = (endTime - startTime) / 1000.0D;
        int elapsedSecs = (int) elapsedSeconds % 60;
        int elapsedMinutes = (int) (elapsedSeconds / 60.0D);

        String withErrors = "";
        if (hadError) {
            withErrors = " with errors";
        }
        System.out.println(new StringBuilder().append("Finished").append(withErrors).append(" in ").append(elapsedMinutes).append(" minutes ").append(elapsedSecs).append(" seconds").toString());
    }
}

/* Location:           C:\Users\文炳\Desktop\ApkIDE3.1\114MS.jar
 * Qualified Name:     tools.wztosql.DumpMobSkills
 * JD-Core Version:    0.6.2
 */